package cn.fl.customer.service.impl;

import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstNpBscInfoService;
import cn.fl.customer.transform.CstNpBscInfoVOTransform;
import cn.fl.customer.transform.CstNpSocialRlVOTransform;
import cn.fl.customer.vo.CstNpBscInfoVO;
import cn.fl.customer.vo.CstNpSocialRlVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjCstAccInfoQueryCondition;
import com.alibaba.fastjson.JSON;
import fl.customer.facade.customer.api.ICustomerModifyApiService;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstNpBscInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class CstNpBscInfoServiceImpl implements ICstNpBscInfoService {

	/**
	 * 日志
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CstNpBscInfoServiceImpl.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ICustomerQueryApiService customerQueryApiService;
	
	@Autowired
	private ICustomerModifyApiService customerModifyApiService;

	@Override
	public CstNpBscInfoVO selectById(Long id) {
		if(StringUtils.isEmpty(id))return null;
		CstNpBscInfoDTO dto = customerQueryApiService.getCstNpBscInfoById(id);
		return CstNpBscInfoVOTransform.toVO(dto);
	}

	/*@Override
	public Long add(CstNpBscInfoVO entity) {
		if(null == entity)return null;
		return this.cstNpBscInfoApiService.add(CstNpBscInfoVOTransform.toDTO(entity));
	}*/

	/*@Override
	public boolean removeById(Long id) {
		if(StringUtils.isEmpty(id))return false;
		return this.cstNpBscInfoApiService.removeById(id);
	}*/

	@Override
	public Long add(CstNpBscInfoVO entity) {
		if(null == entity)return null;
		return this.customerModifyApiService.add(CstNpBscInfoVOTransform.toDTO(entity));
	}

	/*@Override
	public boolean removeById(Long id) {
		if(StringUtils.isEmpty(id))return false;
		return this.cstNpBscInfoApiService.removeById(id);
	}*/

	@Override
	public boolean modify(CstNpBscInfoVO entity) {
		if(null == entity)return false;
		return this.customerModifyApiService.modifyCstNpBscInfo(CstNpBscInfoVOTransform.toDTO(entity));
	}

	/*@Override
	public Pager searchList(Pager page) {
		if(null == page)page = new Pager();
		return this.cstNpBscInfoApiService.searchList(page);
	}*/

	/*@Override
	public Pager searchList(Pager page, CstNpBscInfoVO entity) {
		if(null == entity) return page;
		if(null == page)page = new Pager();
		return this.cstNpBscInfoApiService.searchList(page);
	}*/

	@Override
	public boolean modifyCstNpSocialRlForSpouse(CstNpSocialRlVO entity){
		if(null == entity)return false;
		return this.customerModifyApiService.modifyCstNpSocialRlForSpouse(CstNpSocialRlVOTransform.toDTO(entity));
	}

    /**
     * 修改自然人信息
     * @param prjCstAccInfoQC 客户账户信息
     * @return
     */
    @Override
    public boolean modifyCstNpBscInfo(PrjCstAccInfoQueryCondition prjCstAccInfoQC) throws BaseException {

        //根据客户id查询 客户信息
        CstNpBscInfoVO cstNpBscInfoVO = this.selectById(prjCstAccInfoQC.getCstId());
        boolean modify = false;
        if(null != cstNpBscInfoVO){
            //修改标识
            boolean flag = false;
            CstNpBscInfoVO npBscInfoVO = new CstNpBscInfoVO();
            npBscInfoVO.setId(cstNpBscInfoVO.getId());

            //身份证签发机关
            if(!Objects.equals(prjCstAccInfoQC.getLssueOffice(),cstNpBscInfoVO.getIssuingOrg())){
                npBscInfoVO.setIssuingOrg(prjCstAccInfoQC.getLssueOffice());
                flag = true;
            }
            //postAddr
            if(!Objects.equals(prjCstAccInfoQC.getPostAddr(),cstNpBscInfoVO.getPostAddr())){
                npBscInfoVO.setPostAddr(prjCstAccInfoQC.getPostAddr());
                flag = true;
            }

            //通讯地址-省
            if(!Objects.equals(prjCstAccInfoQC.getPostProvince(),cstNpBscInfoVO.getPostProvince())){
                npBscInfoVO.setPostProvince(prjCstAccInfoQC.getPostProvince());
                flag = true;
            }

            //通讯地址-市
            if(!Objects.equals(prjCstAccInfoQC.getPostCity(),cstNpBscInfoVO.getPostCity())){
                npBscInfoVO.setPostCity(prjCstAccInfoQC.getPostCity());
                flag = true;
            }

            //通讯地址-县
            if(!Objects.equals(prjCstAccInfoQC.getPostCounty(),cstNpBscInfoVO.getPostCounty())){
                npBscInfoVO.setPostCounty(prjCstAccInfoQC.getPostCounty());
                flag = true;
            }

            //户籍地址-省
            if(!Objects.equals(prjCstAccInfoQC.getDomicileProvince(),cstNpBscInfoVO.getDomicileProvince())){
                npBscInfoVO.setDomicileProvince(prjCstAccInfoQC.getDomicileProvince());
                flag = true;
            }

            //户籍地址-市
            if(!Objects.equals(prjCstAccInfoQC.getDomicleCity(),cstNpBscInfoVO.getDomicleCity())){
                npBscInfoVO.setDomicleCity(prjCstAccInfoQC.getDomicleCity());
                flag = true;
            }

            //户籍详细地址
            if(!Objects.equals(prjCstAccInfoQC.getIdAddr(),cstNpBscInfoVO.getIdAddr())){
                npBscInfoVO.setIdAddr(prjCstAccInfoQC.getIdAddr());
                flag = true;
            }

            //民族
            if(!Objects.equals(prjCstAccInfoQC.getNation(),cstNpBscInfoVO.getNation())){
                npBscInfoVO.setNation(prjCstAccInfoQC.getNation());
                flag = true;
            }

            //证件有效起始日期
            if(!Objects.equals(prjCstAccInfoQC.getCertValidSDt(),cstNpBscInfoVO.getCertValidSDt())){
                npBscInfoVO.setCertValidSDt(prjCstAccInfoQC.getCertValidSDt());
                flag = true;
            }

            //证件有效终止日期
            if(!Objects.equals(prjCstAccInfoQC.getCertValidEDt(),cstNpBscInfoVO.getCertValidEDt())){
                npBscInfoVO.setCertValidEDt(prjCstAccInfoQC.getCertValidEDt());
                flag = true;
            }

            //学历
            if(!Objects.equals(prjCstAccInfoQC.getHighDgCd(),cstNpBscInfoVO.getHighDgCd())){
                npBscInfoVO.setHighDgCd(prjCstAccInfoQC.getHighDgCd());
                flag = true;
            }

            Long mrIncMoney = DecimalUtils.convertYuanToFen(Double.valueOf(prjCstAccInfoQC.getMonthInc()));
            // 月收入转换年收入
            Double yrInc = DecimalUtils.mul(mrIncMoney.doubleValue(), 12d);
            if (!Objects.equals(cstNpBscInfoVO.getYrInc().doubleValue(), yrInc)) {
                npBscInfoVO.setYrInc(yrInc.longValue());
                flag = true;
            }

            //有信息变更 更新变更时间和变更人
            if(flag){
                npBscInfoVO.setMdfTm(new Date());//更新时间
                npBscInfoVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());//更新人
                BaseLogUtils.newLogger("修改自然人信息cstId:{}, cstNpBscInfoVO:{}", prjCstAccInfoQC.getCstId(), JSON.toJSONString(cstNpBscInfoVO)).info();
                modify = this.modify(npBscInfoVO);
                BaseLogUtils.newLogger("修改自然人信息cstId:{}, modify:{}", prjCstAccInfoQC.getCstId(), modify).info();
            }
        }
        return modify;
    }


}
